#include "HandshakeCmd.h"
#include "System/System.h"
#include "Device/WXTPlatformDevice.h"

//#include <Encrypt/SM4Encrypt.h>

HandshakeCmd::HandshakeCmd(QObject *parent) :
    BaseCmd(parent)
{
    this->m_protocolType = ProtocolType::CloudProtocol;
    this->m_functionCode = FunctionCode;
    m_clientRandom.clear();
    m_ivecRandom.clear();
    m_appVersion.clear();
    m_systemName.clear();
    m_systemVersion.clear();
    m_clientType=0;
}

Package HandshakeCmd::toPackage()
{
    Package package;
    QByteArray body;

    quint16 strLength;

    strLength = m_clientRandom.size();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_clientRandom);

    strLength = m_ivecRandom.size();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_ivecRandom);

    strLength = m_appVersion.length();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_appVersion);

    strLength = m_systemName.length();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_systemName);

    strLength = m_systemVersion.length();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_systemVersion);

    body.append(m_clientType);

   // SM4Encrypt *sm4=SM4Encrypt::getInstance();
   // body= sm4->EncryptBySM4(body);
    //body = encrypt(body,KeyType::M4);

   //  qDebug()<<"QByteArray encrypt   KeyType:M4";
     System *system = System::getInstance();
     QMap<QString,BaseDevice*>  deviceInstanceMap = system->getDeviceInstanceMap();
     if(!deviceInstanceMap.contains("WXTPlatformDevice"))
     {
         qDebug()<<"Error:WXTPlatformDevice is not exist!";
         return package;
     }
      qDebug()<<"QByteArray encrypt11   KeyType:M4";
     BaseDevice* meta = deviceInstanceMap.value("WXTPlatformDevice");
     WXTPlatformDevice* meta1 = dynamic_cast<WXTPlatformDevice*>(meta);
      qDebug()<<"QByteArray encrypt22   KeyType:M4";
     QMetaObject::invokeMethod((QObject*)meta1,"messageEncrypt");
      qDebug()<<"QByteArray encrypt33   KeyType:M4";


    package.setId(m_cmdId);
    package.setFunction(FunctionCode);
    package.setBody(body);

    return package;
}

void HandshakeCmd::unPackage(Package package)
{
    m_cmdId = package.id();

    QByteArray body;
    quint16 strLength,index = 0;
    body=package.body();
   // body = decrypt(package.body(),KeyType::RSA);

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    m_clientRandom = body.mid(index,strLength);
    index += strLength;

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    m_ivecRandom = body.mid(index,strLength);
    index += strLength;

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    m_appVersion = body.mid(index,strLength);
    index += strLength;

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    m_systemName = body.mid(index,strLength);
    index += strLength;

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    m_systemVersion = body.mid(index,strLength);
    index += strLength;

    m_clientType = body.mid(index,1).toHex().toInt(0,16);
}

QByteArray HandshakeCmd::clientRandom() const
{
    return m_clientRandom;
}

void HandshakeCmd::setClientRandom(const QByteArray &clientRandom)
{
    m_clientRandom = clientRandom;
}
QByteArray HandshakeCmd::ivecRandom() const
{
    return m_ivecRandom;
}

void HandshakeCmd::setIvecRandom(const QByteArray &ivecRandom)
{
    m_ivecRandom = ivecRandom;
}
QString HandshakeCmd::appVersion() const
{
    return m_appVersion;
}

void HandshakeCmd::setAppVersion(const QString &appVersion)
{
    m_appVersion = appVersion;
}
QString HandshakeCmd::systemVersion() const
{
    return m_systemVersion;
}

void HandshakeCmd::setSystemVersion(const QString &systemVersion)
{
    m_systemVersion = systemVersion;
}
QString HandshakeCmd::systemName() const
{
    return m_systemName;
}

void HandshakeCmd::setSystemName(const QString &systemName)
{
    m_systemName = systemName;
}
quint8 HandshakeCmd::clientType() const
{
    return m_clientType;
}

void HandshakeCmd::setClientType(const quint8 &clientType)
{
    m_clientType = clientType;
}






